using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;using THSERP.BusinessEntities;using THSERP.BaseProvider;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace THSERP.Modules.CompanyConstant
{
    public partial class guiVATConfiguration : THSERPScreen
    {
        private List<GEVATsInfo> lstGEVATsInfo = new List<GEVATsInfo>();
        private List<GEVATRegionsInfo> lstGEVATRegionsInfo = new List<GEVATRegionsInfo>();
        public guiVATConfiguration()
        {
            InitializeComponent();
            InitVATToGrid();
            InitVATRegionsToGrid();
        }

        #region Utility Functions
        private void InitVATToGrid()
        {
            GEVATsController objGEVATsController = new GEVATsController();
            DataSet dsGEVATs = objGEVATsController.GetAllObjects();
            foreach (DataRow rowGEVAT in dsGEVATs.Tables[0].Rows)
            {
                GEVATsInfo objGEVATsInfo = (GEVATsInfo)new GEVATsController().GetObjectFromDataRow(rowGEVAT);
                if (objGEVATsInfo.GEVATID > 0)
                    lstGEVATsInfo.Add(objGEVATsInfo);
            }
            fld_dgcGEVATs.DataSource = lstGEVATsInfo;
        }

        private void InitVATRegionsToGrid()
        {
            GEVATRegionsController objGEVATRegionsController = new GEVATRegionsController();
            DataSet dsGEVATRegions = objGEVATRegionsController.GetAllObjects();
            foreach (DataRow rowGEVATRegion in dsGEVATRegions.Tables[0].Rows)
            {
                GEVATRegionsInfo objGEVATRegionsInfo = (GEVATRegionsInfo)new GEVATRegionsController().GetObjectFromDataRow(rowGEVATRegion);
                if (objGEVATRegionsInfo.GEVATRegionID > 0)
                {
                    lstGEVATRegionsInfo.Add(objGEVATRegionsInfo);
                }
            }
            fld_dgcGEVATRegions.DataSource = lstGEVATRegionsInfo;
        }
        #endregion

        private void fld_dgvGEVATs_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if(e.Column==colGEVATPercentValue)
            {
                GEVATsController objGEVATsController = new GEVATsController();
                int iGEVATID= (int)fld_dgvGEVATs.GetRowCellValue(e.RowHandle,colGEVATID);
                GEVATsInfo objGEVATsInfo = (GEVATsInfo)objGEVATsController.GetObjectByID(iGEVATID);
                objGEVATsInfo.GEVATPercentValue = Convert.ToDouble(fld_dgvGEVATs.GetRowCellValue(e.RowHandle, e.Column));
                objGEVATsController.UpdateObject(objGEVATsInfo);
            }
            if (e.Column == colGEVATName)
            {
                GEVATsController objGEVATsController = new GEVATsController();
                int iGEVATID = (int)fld_dgvGEVATs.GetRowCellValue(e.RowHandle, colGEVATID);
                GEVATsInfo objGEVATsInfo = (GEVATsInfo)objGEVATsController.GetObjectByID(iGEVATID);
                objGEVATsInfo.GEVATName = fld_dgvGEVATs.GetRowCellValue(e.RowHandle, e.Column).ToString();
                objGEVATsController.UpdateObject(objGEVATsInfo);
            }
        }

        private void fld_dgvGEVATRegions_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            GEVATRegionsController objGEVATRegionsController = new GEVATRegionsController();
            int iGEVATRegionID = (int)fld_dgvGEVATRegions.GetRowCellValue(e.RowHandle, colGEVATRegionID);
            GEVATRegionsInfo objGEVATRegionsInfo = (GEVATRegionsInfo)objGEVATRegionsController.GetObjectByID(iGEVATRegionID);
            if (objGEVATRegionsInfo != null)
            {
                if (e.Column == colGEVATRegionName)
                    objGEVATRegionsInfo.GEVATRegionName = fld_dgvGEVATRegions.GetRowCellValue(e.RowHandle, colGEVATRegionName).ToString();
                if (e.Column == colGEVATRegionDesc)
                    objGEVATRegionsInfo.GEVATRegionDesc = fld_dgvGEVATRegions.GetRowCellValue(e.RowHandle, colGEVATRegionDesc).ToString();
                objGEVATRegionsController.UpdateObject(objGEVATRegionsInfo);
            }
        }
    }
}